package cn.com.elite;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

public class EliteBean2 {
	private static final Log log=LogFactory.getLog(EliteBean2.class);
    private DataSource dataSource;
    private DataSourceTransactionManager tran;
    

	public DataSource getDataSource() {
		return dataSource;
	}


	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public DataSourceTransactionManager getTran() {
		return tran;
	}


	public void setTran(DataSourceTransactionManager tran) {
		this.tran = tran;
	}

    private String getcontent(Object[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			if(a[i]!=null){
    				b=b+"a["+i+"]："+a[i].toString()+"-";	
    			}else{
    				b=b+"a["+i+"]：null-";	
    			}
    		}
    	}
    	return b;
    }
    private String getcontent1(int[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			b=b+"a["+i+"]："+a[i]+"-";
    		}
    	}
    	return b;
    }
	public String insertPersonUseExecute(String sql,int[] types,Object[] params,String sql1,String flag,String sql2,int[] types2,Object[] params2,String flag2){
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		try{
			if(flag2!=null&&flag2.equals("1")){
				log.info("sql2："+sql2);
				log.info("params1："+getcontent(params2));
				int result1=jdbcTemplate.update(sql2,params2,types2);
				log.info("result1:"+result1);
			}
			log.info("params："+getcontent(params));
			log.info("sql:"+sql);
			int result=jdbcTemplate.update(sql,params,types);
			log.info("result:"+result);
			if(result==1&&flag.equals("1")){
				sql1=sql1+"'"+params[Integer.parseInt(flag)]+"'";
				return sql1;
			}else{
				log.debug("result:"+result);
				return "0";  
			}
		}catch(Exception e){
			 log.error("出错types："+getcontent1(types));
			 log.error("出错params："+getcontent(params));
			 log.error("出错types1："+getcontent1(types2));
			 log.error("出错params1："+getcontent(params2));
			 log.error("出错SQL："+sql);
			 log.error("insertPersonUseExecute出错:"+EliteUtil.getERRO(e));  
			 return "0";  
		}
		
	}
	public int update(String sql)throws Exception{
		log.debug("sql:"+sql);
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate.update(sql);
	}
    public String doProcess() {
    	log.info("---------doProcess------------");
    	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        Object param2Value=jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(Connection con){
                    String storedProc = "{call BuildCustomer_GUID()}";// 调用的sql
                    CallableStatement cs = null;
                    try{
                   	 cs = con.prepareCall(storedProc);
                   }catch(Exception e){
                   	log.info("cs = con.prepareCall(storedProc)错误:"+e);
                   }
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)  {
                    try{
                    	cs.execute();
                    }catch(Exception e){
                    	log.info("cs.execute()错误:"+e);
                    }
                    return null;
            }
        });
        log.info("param2Value:"+param2Value); 
        return param2Value+"";
    } 
	public int deltetsql(String sql){
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		try{
			return jdbcTemplate.update(sql);
		}catch(Exception e){
			 log.error("出错SQL："+sql);
			 log.error("deltetsql出错:"+EliteUtil.getERRO(e));  
			 return 0;
		} 
	}
    

}
